package com.abel.bigwater.dataserver.model.qdies

import com.abel.bigwater.data.BcdString
import com.alibaba.fastjson.JSON
import io.netty.buffer.Unpooled
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test
import org.slf4j.LoggerFactory

class D10PacketRealTest {
    @Test
    fun testParsePulseHist1() {
        val buf = Unpooled.buffer().writeBytes(BcdString.toByteArray(HEX_1))

        val p = D10Packet()
        assertTrue("should be okay with header", p.parse(buf))
        assertTrue("first login", p.includeLogin)
        lgr.info(JSON.toJSONString(p, true))

        assertFalse("not enough", p.parse(buf))
    }

    @Test
    fun testParsePulseHist2() {
        val buf = Unpooled.buffer().writeBytes(BcdString.toByteArray(HEX_2))

        val p = D10Packet()
        assertTrue("should be okay with header", p.parse(buf))
        assertTrue("first login", p.includeLogin)
        lgr.info(JSON.toJSONString(p, true))

        assertFalse("not enough", p.parse(buf))
    }

    @Test
    fun testParsePulseHist3() {
        val buf = Unpooled.buffer().writeBytes(BcdString.toByteArray(HEX_3))

        val p = D10Packet()
        assertTrue("should be okay with header", p.parse(buf))
        assertTrue("first login", p.includeLogin)
        lgr.info(JSON.toJSONString(p, true))

        assertFalse("not enough", p.parse(buf))
    }

    @Test
    fun testParseState1() {
        val buf = Unpooled.buffer().writeBytes(BcdString.toByteArray(HEX_4))

        val p = D10Packet()
        assertTrue("should be okay with header", p.parse(buf))
        assertFalse("first login", p.includeLogin)
        lgr.info(JSON.toJSONString(p, true))

        assertTrue("enough still", p.parse(buf))
    }

    @Test
    fun testHeadFail() {
        val buf = Unpooled.buffer().writeBytes(BcdString.toByteArray(HEX_0))

        val p = D10Packet()
        assertFalse("should be okay with header", p.parse(buf))
        assertTrue("first login", p.includeLogin)
        lgr.info(JSON.toJSONString(p, true))
    }

    companion object {
        private val lgr = LoggerFactory.getLogger(D10PacketRealTest::class.java)
        const val HEX_0 = """49 45 53 44 31 30 71 90 39 94 fe fe 68 71"""

        const val HEX_1 = """49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 10 15 20 0b 02 00 00 80
3f ae 16 45 20"""

        const val HEX_2 = """49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 10 15 25 0b 02 66 66 a2
42 b8 16 46 18"""

        const val HEX_3 = """49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 10 23 00 0b 02 33 33 ba
42 1d 16 47 21"""

        const val HEX_4 = """fe fe 68 71 90 39 94 1e 14 00 00 00 00 00 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 18 16 
fe fe 68 71 90 39 94 1e 14 00 00 00 00 00 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 18 16"""
    }
}